package com.jiangruan.javaApiProject.service;


import com.alibaba.fastjson.JSONObject;
import com.jiangruan.javaApiProject.Constants;
import com.jiangruan.javaApiProject.net.HttpUtils;

import java.util.Base64;
import java.util.HashMap;

/**
 * @Author 默
 * @Date 2021/12/1419:15
 * @desc 该类用于实现比特币客户端的getbestBlockHash的rpc功能
 * @Description
 */
public class GetBestBlockHashService {

    public static String getBestBlockHash(){
        //准备用户身份信息
        String author = Constants.BITCOIN_RPC_USER+":"+Constants.BITCOIN_RPC_PASSWORD;
        Base64.Encoder encoder = Base64.getEncoder();
        String authorBase64 = encoder.encodeToString(author.getBytes());


        //准备请求头信息
        HashMap<String, String> header = new HashMap<>();
        header.put("ContentType","application/json");
        header.put("Authorization","Basic " + authorBase64);

        //rpc请求格式
        HashMap<String, Object> data = new HashMap<>();
        data.put("id",10001);
        data.put("method","getbestblockhash");
        data.put("params",null);
        data.put("json_rpc","2.0");
        byte[] bytes = JSONObject.toJSONBytes(data);


        String result = HttpUtils.post(Constants.BITCOIN_RPC_URL, header, bytes);

        return result;

    }
}
